package com.example.basic;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class ProductFragment extends Fragment {
  private Product mProduct;
  public final static String EXTRA_PRODUCT_ID = "com.example.basic.ProductFragment.product_id";

  public static ProductFragment newInstance(int productId) {
    Bundle args = new Bundle();
    args.putInt(EXTRA_PRODUCT_ID, productId);

    ProductFragment fragment = new ProductFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    int productId = getArguments().getInt(EXTRA_PRODUCT_ID);
    // Get a default product from the inventory
    Inventory inventory = Inventory.getInstance(getActivity());
    mProduct = inventory.getProductById(productId);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_product, container, false);

    TextView productName = (TextView)rootView.findViewById(R.id.product_name);
    productName.setText(mProduct.getName());

    TextView productDescription = (TextView)rootView.findViewById(R.id.product_description);
    productDescription.setText(mProduct.getDescription());

    Button button = (Button)rootView.findViewById(R.id.product_cart);
    button.setOnClickListener(new View.OnClickListener(){
      @Override
      public void onClick(View v) {
        FragmentManager fm = getActivity().getSupportFragmentManager();
        EventDialogFragment eventDialog = EventDialogFragment.newInstance(mProduct.getName());
        eventDialog.show(fm, "EventDialogFragment");
      }
    });

    return rootView;
  }
}
